package com.itrip.controller;

import com.itrip.dto.ResponseDTO;
import com.itrip.pojo.ItripUser;
import com.itrip.service.LoginService;
import com.itrip.utils.MD5;
import com.itrip.vo.LoginView;
import io.swagger.annotations.*;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(tags = "登陆接口")
@ApiResponses({
        @ApiResponse(code = 100, message = "获取数据成功", response = ResponseDTO.class),
        @ApiResponse(code = 101, message = "获取数据失败", response = ResponseDTO.class),
        @ApiResponse(code = 102, message = "获取数据异常", response = ResponseDTO.class)
})
@RestController
public class LoginController {
    @Resource
    private LoginService loginService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;  //注入redis

    @ApiOperation("登陆接口")
    @PostMapping("/login")
    public ResponseDTO login(LoginView loginView){
        //通过用户名和密码查询用户
        ItripUser itripUser= loginService.login(loginView);
        //对密码进行加密处理
        String password= MD5.md5(loginView.getUserPassword());
        System.out.println(itripUser);
        if(itripUser!=null){
            if (itripUser.getUserPassword().equals(password)){
                //生成登陆令牌的token
                String token = UUID.randomUUID().toString();
                //在redis中存入用户信息,过期时间为6小时
                redisTemplate.opsForValue().set(token,itripUser,6,TimeUnit.HOURS);
                //返回数据
                return new ResponseDTO(100,"获取数据成功",token);
            }
            return new ResponseDTO(101,"获取数据失败,用户密码错误",null);
        }
        return new ResponseDTO(101,"获取数据失败,用户部存在",null);
    }


    @ApiOperation("注销接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登陆令牌",dataType = "String",paramType = "header")
    })
    @GetMapping("/view/loginout")
    public ResponseDTO loginout(HttpServletRequest request){
        //删除redis中的token
        String token = request.getHeader("token");
        System.out.println(token);
        /*Object o = redisTemplate.opsForValue().get(token);
        System.out.println(o);*/
        redisTemplate.delete(token);
        return new ResponseDTO(100,"注销成功",null);
    }


    @ApiOperation("根据token获取redis中用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户登陆令牌",dataType = "String",paramType = "header")
    })
    @GetMapping("/view/getLoginUser")
    public ResponseDTO getLoginUser(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        ItripUser itripUser = (ItripUser)redisTemplate.opsForValue().get(token);
        return new ResponseDTO(100,"用户获取成功",itripUser);
    }

}
